//
//  shareView.m
//  TryShopping
//
//  Created by panquanReabam.com on 16/6/17.
//  Copyright © 2016年 RuiBen. All rights reserved.
//

#import "ShareView.h"
//#import "UMSocial.h"
#import "UMShare/UMShare.h"
#import <UShareUI/UShareUI.h>
@interface ShareView()<UIScrollViewDelegate> //,UMSocialUIDelegate
@end

@implementation ShareView
{
  UIImage *image1;
}
@synthesize mDelegate;


- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        UIScrollView*shareScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, ScreenWidth,245)];
        shareScroll.delegate = self;
        shareScroll.userInteractionEnabled = YES;
        shareScroll.scrollEnabled = YES;
        shareScroll.showsVerticalScrollIndicator = FALSE;
        shareScroll.showsHorizontalScrollIndicator = FALSE;
        shareScroll.backgroundColor=[UIColor whiteColor];
        [self  addSubview:shareScroll];
        NSArray*nameBtn=[[NSArray alloc]initWithObjects:@"发送给朋友",@"分享到朋友圈",nil];
        NSArray *arrayimage = @[@"faweixin",@"fenxiangpenyouquan"];
        for (int i = 0; i<2; i++) {
            UIButton *nameButcenter = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3*i,   80*ScreenHeight/1334+15, ScreenWidth/3, 60)];

            [shareScroll addSubview:nameButcenter];
            UIButton *nameBut = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3*i,   nameButcenter.bottom+20, ScreenWidth/3, 14)];
            [nameBut setTitle: [nameBtn objectAtIndex:i]  forState:UIControlStateNormal];
            nameBut.tag=201+i;
            [nameBut setTitleColor:shenhuiColor forState:0];
            nameBut.titleLabel.font=[UIFont systemFontOfSize:14];
            [nameBut addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
            [shareScroll addSubview:nameBut];
            UIButton *wuyebuttonimage = [UIButton buttonWithType:UIButtonTypeCustom];
            wuyebuttonimage.frame = CGRectMake(ScreenWidth/3*i,  10, 60, 60);

            wuyebuttonimage.tag = 201+i;
            wuyebuttonimage.center=nameButcenter.center;
            [wuyebuttonimage setImage:[UIImage imageNamed: [arrayimage objectAtIndex:i]] forState:UIControlStateNormal];
            [wuyebuttonimage addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
            [shareScroll addSubview:wuyebuttonimage];
        }
//        UIButton*cancel = [[UIButton alloc] initWithFrame:CGRectMake(0,200,  ScreenWidth, 45)];
//        [cancel setTitle:@"取消" forState:UIControlStateNormal];
//        cancel.titleLabel.font=[UIFont systemFontOfSize:15];
//        cancel.backgroundColor=[UIColor whiteColor];
//        [cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//        [cancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:cancel];
//        UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, ScreenWidth-20, 0.40)];
//        line.backgroundColor=qianhuiColor;
//        [cancel addSubview:line];
        
        if (is_iPhoneX) {
            UIView *backView = [[UIView alloc] init];
            backView.frame = CGRectMake(0,200,  ScreenWidth, 45+34);
            backView.backgroundColor = [UIColor whiteColor];
            [self addSubview:backView];
            
            UIButton*cancel = [[UIButton alloc] initWithFrame:CGRectMake(0,0,  ScreenWidth, 45)];
            [cancel setTitle:@"取消" forState:UIControlStateNormal];
            cancel.titleLabel.font=[UIFont systemFontOfSize:15];
            cancel.backgroundColor=[UIColor whiteColor];
            [cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [cancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
            [backView addSubview:cancel];
            UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, ScreenWidth-20, 0.40)];
            line.backgroundColor=qianhuiColor;
            [backView addSubview:line];
        } else{
            UIButton*cancel = [[UIButton alloc] initWithFrame:CGRectMake(0,200,  ScreenWidth, 45)];
            [cancel setTitle:@"取消" forState:UIControlStateNormal];
            cancel.titleLabel.font=[UIFont systemFontOfSize:15];
            cancel.backgroundColor=[UIColor whiteColor];
            [cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [cancel addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:cancel];
            UILabel*line=[[UILabel alloc]initWithFrame:CGRectMake(10, 0, ScreenWidth-20, 0.40)];
            line.backgroundColor=qianhuiColor;
            [cancel addSubview:line];
        }

    }
    return self;
}
-(void)cancel
{
    [mDelegate cancel:@""];
}

-(void)share:(UIButton*)sender{
//    image1 = [UIImage imageNamed:@"桌面启动图标（100）.png"];
    if ([[self.dataDict objectForKey:@"imageUrl"] length]>0) {

        NSURL *url = [NSURL URLWithString:[self.dataDict objectForKey:@"imageUrl"]];
        NSData *data = [NSData dataWithContentsOfURL:url];
        image1 = [[UIImage alloc] initWithData:data];
        if (data==NULL) {
            NSString *str1=[NSString stringWithFormat:@"%d",TARGET_VERSION];
            NSString *plistPath1 = [[NSBundle mainBundle] pathForResource:str1 ofType:@"plist"];
            NSDictionary *dictionary1 = [[NSDictionary alloc] initWithContentsOfFile:plistPath1];
            NSString *logoStr = [dictionary1 objectForKey:@"logoStr"];
            image1 = [UIImage imageNamed:logoStr];
        }
    }else{

        NSString *str1=[NSString stringWithFormat:@"%d",TARGET_VERSION];
        NSString *plistPath1 = [[NSBundle mainBundle] pathForResource:str1 ofType:@"plist"];
        NSDictionary *dictionary1 = [[NSDictionary alloc] initWithContentsOfFile:plistPath1];
        NSString *logoStr = [dictionary1 objectForKey:@"logoStr"];
        image1 = [UIImage imageNamed:logoStr];
    }


    // 分享给朋友
    if (sender.tag==201) {
        
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        //创建网页内容对象
        NSString *title = [NSString stringWithFormat:@"%@", [self.dataDict objectForKey:@"title"]];
        NSString *shareDescription = [NSString stringWithFormat:@"%@", [self.dataDict objectForKey:@"shareDescription"]];
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:shareDescription thumImage:image1];
        //设置网页地址
        shareObject.webpageUrl = _shareUrl;
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
            if (error) {
                UMSocialLogInfo(@"************Share fail with error %@*********",error);
                NSLog(@"%@", error);
            }else{
                if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                    UMSocialShareResponse *resp = data;
                    //分享结果消息
                    UMSocialLogInfo(@"response message is %@",resp.message);
                    //第三方原始返回的数据
                    UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                }else{
                    UMSocialLogInfo(@"response data is %@",data);
                }
            }
        }];
    }

    // 分享到朋友圈
    if (sender.tag==202) {

        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        //创建网页内容对象
        NSString *title = [NSString stringWithFormat:@"%@", [self.dataDict objectForKey:@"title"]];
        NSString *shareDescription = [NSString stringWithFormat:@"%@", [self.dataDict objectForKey:@"shareDescription"]];
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:title descr:shareDescription thumImage:image1];
        //设置网页地址
        shareObject.webpageUrl = _shareUrl;
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatTimeLine messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
            if (error) {
                UMSocialLogInfo(@"************Share fail with error %@*********",error);
                NSLog(@"%@", error);
            }else{
                if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                    UMSocialShareResponse *resp = data;
                    //分享结果消息
                    UMSocialLogInfo(@"response message is %@",resp.message);
                    //第三方原始返回的数据
                    UMSocialLogInfo(@"response originalResponse data is %@",resp.originalResponse);
                }else{
                    UMSocialLogInfo(@"response data is %@",data);
                }
            }
        }];
        
    }

}
-(void)ShareRecords{
    [self.mDelegate successfullyReturns:@""];
}

@end
